﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SHNOPData.SHNOPModel;
using SHNOPData.common;

namespace SHNOPData.ImportExcel
{
    /// <summary>
    /// 解析二维数据，导入数据库
    /// </summary>
    public class ImportZhandian:IImportExcel
    {
        public void Import(string[,] array)
        {
            string retMsg = "";
            string errorMsg = "";
            int successCount = 0;
            List<ZhanDianInfo> listZd = GetZhanDianInfoList(array, out errorMsg);
            int failCount = array.GetLength(0) - listZd.Count-1;
            foreach (var item in listZd)
            {
                bool isValidId=IsValidPlanID(item,ref retMsg);
                if (!isValidId)
                {
                    failCount++;
                    continue;
                }

                bool result = AppDBMng.ImportZhanDian(item, ref retMsg);
                if (result)
                {
                    successCount += 1;
                }
                else
                {
                    failCount++;
                    retMsg = retMsg + "第" + item.No + "条记录导入失败\n";
                }
            }
            //session 用于客户端获取
            if (failCount == 0)
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条记录";
            }
            else
            {
                System.Web.HttpContext.Current.Session["batchImport"] = "成功导入" + successCount.ToString() + "条,失败" + failCount.ToString() + "条\n" + retMsg;
            }
           
        }
        private bool IsValidPlanID(ZhanDianInfo item,ref string retMsg)
        {
            if (item.GuihuaID2G.Length > 0)
            {
                item.GuihuaIDTD = null;
                return true;
            }
            else if (item.GuihuaID2G.Length == 0 && item.GuihuaIDTD.Length == 0)
            {
                retMsg = retMsg + "第" + item.No + "条记录导入失败,规划编号必须至少填写一个\n";
            }
            return false;
        }
  
        private float? Convert2Float(string info)
        {
            float outResult = 0;
            float? valOrNull = (info.Length != 0 && float.TryParse(info, out outResult)) ? outResult : (float?)null;
            return valOrNull;
        }
        /// <summary>
        /// 将EXcell解析的二维数组填充站点信息列表，每一列代表一行数据
        /// </summary>
        /// <param name="array"></param>
        /// <param name="msg"></param>
        /// <returns></returns>
        public  List<ZhanDianInfo> GetZhanDianInfoList(string[,] array, out string msg)
        {
            msg = "";
            List<ZhanDianInfo> zdList = new List<ZhanDianInfo>();
            if (array != null)
            {
                for (int i = 1; i < array.GetLength(0); i++)
                {
                    int j = 0;
                    while (j < array.GetLength(1))
                    {
                        try
                        {
                            ZhanDianInfo zd = new ZhanDianInfo();
                            zd.No = array[i, j++];
                            if (zd.No.Length==36)
                            {
                                zd.zhanDianID = new Guid(zd.No);
                            }
                            #region 站点
                            zd.company = array[i, j++];
                            zd.GuihuaID2G = array[i, j++];
                            zd.GuihuaIDTD = array[i, j++];
                            zd.isZhanDan = 1;//array[i, j++].Trim() == "是" ? 1 : 0;//按照需求自动生成是
                            zd.orderNum = array[i, j++];
                            zd.realityLongitude = Convert2Float(array[i, j++]);// array[i, j].Length == 0 ? float.NaN : float.TryParse(array[i, j++], out outResult) ? outResult : float.NaN;

                            zd.realityLatitude = Convert2Float(array[i, j++]);
                            zd.realityHeight =array[i, j++];
                            zd.address = array[i, j++];
                            //是否同意
                            zd.isAgree = array[i, j++];
                            zd.isFuHeZhanCha = array[i, j++].Trim() == "是" ? 1 : 0;
                            string agreeScale = array[i, j++];
                            if (agreeScale.Contains("%"))
                            {
                                agreeScale = agreeScale.Replace("%", "");
                                agreeScale = "0." + agreeScale;
                            }
                            zd.agreeScale = Convert2Float(agreeScale);
                            zd.ReplyContent = array[i, j++];
                            zd.planningFXJiao = array[i, j++];
                            zd.planningPZ = array[i, j++];
                            zd.planningJXQJiao = array[i, j++];
                            zd.planningDZQJiao = array[i, j++];
                            zd.HouXuType = array[i, j++];
                            zd.HouXuNum = array[i, j++];
                            zd.HouXuFangAn = array[i, j++];
                            zd.HouXuAddr = array[i, j++];
                            zd.HouXulongitude = Convert2Float(array[i, j++]);
                            zd.HouXulatitude = Convert2Float(array[i, j++]);
                            zd.HouXuGuaGao = array[i, j++];
                            zd.TDOrderNum = array[i, j++];
                            zd.TDIsAgree = array[i, j++];

                            zd.TDIsFuHeZhanCha = array[i, j++].Trim() == "是" ? 1 : 0;
                            zd.TDAgreeScale = Convert2Float(array[i, j++]);
                            zd.TDReplyContent = array[i, j++];
                            zd.TDPlanningFXJiao = array[i, j++];
                            zd.TDPlanningPZ = array[i, j++];
                            zd.TDPlanningJXQJiao = array[i, j++];
                            zd.TDPlanningDZQJiao =array[i, j++];
                            zd.TDHouXuType = array[i, j++];
                            zd.TDHouXuNum = array[i, j++];
                            zd.TDHouXuFangAn = array[i, j++];
                            zd.TDHouXuAddr = array[i, j++];
                            zd.TDHouXulongitude = Convert2Float(array[i, j++]);
                            zd.TDHouXulatitude = Convert2Float(array[i, j++]);
                            zd.TDHouXuGuaGao = array[i, j++];
                           
                            zd.PhysicalSite = array[i, j++];
                            zd.ReplyDate = DateTime.Now.ToString();
                            #endregion
                            zdList.Add(zd);
                        }
                        catch (System.Exception ex)
                        {
                        	//msg
                        }
                        
                    }

                }
            }
            return zdList;
        }
    }
}
